/* Publisher.java
 *
 * Author:	Malcolm Staudacher
 * Date:	Nov 5, 2008
 *
 */

package fhv.itb.ec.jms;

import java.util.Hashtable;
import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Publisher {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// properties for JNDI
		Hashtable<String, String> jmsProperties = new Hashtable<String, String>();
		jmsProperties.put("java.naming.factory.initial",
				"com.sun.jndi.fscontext.RefFSContextFactory");
		jmsProperties.put("java.naming.provider.url", args[0]);

		try {
			// get JNDI initial context
			InitialContext ictxt = new InitialContext(jmsProperties);

			// lookup the topic object
			TopicConnectionFactory cf = (TopicConnectionFactory) ictxt
					.lookup("MyTopicConnFactory");

			// get the (topic) destination
			Topic topic = (Topic) ictxt.lookup("MyTopic");

			// create connection
			TopicConnection con = cf.createTopicConnection();

			// create JMS session
			TopicSession sess = con.createTopicSession(false,
					Session.AUTO_ACKNOWLEDGE);

			// create a topic publisher
			TopicPublisher topicPublisher = sess.createPublisher(topic);
			topicPublisher.setDeliveryMode(DeliveryMode.PERSISTENT);

			// create the "Hello World" message
			TextMessage message = sess.createTextMessage();
			message.setText("Hello World");

			// publish the messages
			topicPublisher.publish(message);

			// print what we did
			System.out.println("published: " + message.getText());

			// close the topic connection
			con.close();

			System.out.println("Connection closed");

		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
